package com.example.my0521.fragments;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.my0521.R;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AlbumDetallFragment extends Fragment {
    OkHttpClient client=new OkHttpClient();
    public AlbumDetallFragment() {
        super(R.layout.fragment_album_detall);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Bundle bundle =getArguments();
        int id=bundle.getInt("id");
        ImageView imageView=view.findViewById(R.id.img);
        TextView a1=view.findViewById(R.id.singers);
        TextView a2=view.findViewById(R.id.area);
        TextView a3=view.findViewById(R.id.releaseDate);
        TextView a4=view.findViewById(R.id.style);
        Request request=new Request.Builder()
                .url("http://johnyu.cn:3000/albums/"+id)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                AlbumDetallFragment.this.getActivity()
                        .runOnUiThread(()->{
                            try {
                                RequestOptions options=new RequestOptions();
                                options.circleCrop();
                                String boby=response.body().string();
                                JSONObject obj=new JSONObject(boby);
                                String imgurl=obj.getString("img");
                                String singers=obj.getString("singers");
                                String area=obj.getString("area");
                                String releaseDate=obj.getString("releaseDate");
                                String style=obj.getString("style");


                                a1.setText("歌手: "+singers);
                                a2.setText("地区: "+area);
                                a3.setText("日期: "+releaseDate);
                                a4.setText("风格: "+style);
                                Glide.with(AlbumDetallFragment.this).load(imgurl)
                                        .apply(options)
                                        .into(imageView);
                            }catch (Exception e){
                                e.printStackTrace();
                            }



                        });


            }
        });


    }
}
